<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CAgent API Connection Test</title>
  <style>
    body {
      font-family: system-ui, -apple-system, sans-serif;
      max-width: 800px;
      margin: 50px auto;
      padding: 20px;
      background: #f5f5f5;
    }
    .container {
      background: white;
      padding: 30px;
      border-radius: 8px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    h1 {
      color: #333;
      margin-bottom: 30px;
    }
    .test-section {
      margin-bottom: 30px;
      padding: 20px;
      background: #f9f9f9;
      border-radius: 4px;
    }
    .test-section h2 {
      margin-top: 0;
      color: #555;
      font-size: 18px;
    }
    button {
      background: #4CAF50;
      color: white;
      border: none;
      padding: 10px 20px;
      border-radius: 4px;
      cursor: pointer;
      font-size: 14px;
      margin-right: 10px;
    }
    button:hover {
      background: #45a049;
    }
    button:disabled {
      background: #ccc;
      cursor: not-allowed;
    }
    .result {
      margin-top: 15px;
      padding: 15px;
      background: white;
      border-radius: 4px;
      border-left: 4px solid #4CAF50;
      font-family: monospace;
      font-size: 12px;
      white-space: pre-wrap;
      word-break: break-all;
    }
    .result.error {
      border-left-color: #f44336;
      background: #ffebee;
    }
    .result.success {
      border-left-color: #4CAF50;
      background: #e8f5e9;
    }
    .status {
      display: inline-block;
      padding: 4px 8px;
      border-radius: 3px;
      font-size: 12px;
      font-weight: bold;
      margin-left: 10px;
    }
    .status.success {
      background: #4CAF50;
      color: white;
    }
    .status.error {
      background: #f44336;
      color: white;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>🚀 CAgent API Connection Test</h1>

    <div class="test-section">
      <h2>1. Health Check</h2>
      <button onclick="testHealth()">Test Health Endpoint</button>
      <div id="health-result"></div>
    </div>

    <div class="test-section">
      <h2>2. List Agent Templates</h2>
      <button onclick="testTemplates()">Test Templates Endpoint</button>
      <div id="templates-result"></div>
    </div>

    <div class="test-section">
      <h2>3. List Agents</h2>
      <button onclick="testListAgents()">Test List Agents</button>
      <div id="agents-result"></div>
    </div>

    <div class="test-section">
      <h2>4. Create Agent</h2>
      <button onclick="testCreateAgent()">Test Create Agent</button>
      <div id="create-result"></div>
    </div>

    <div class="test-section">
      <h2>5. Run All Tests</h2>
      <button onclick="runAllTests()">Run All Tests</button>
    </div>
  </div>

  <script>
    const BASE_URL = 'http://localhost:3000';

    function showResult(elementId, data, isError = false) {
      const element = document.getElementById(elementId);
      const resultDiv = document.createElement('div');
      resultDiv.className = `result ${isError ? 'error' : 'success'}`;
      resultDiv.textContent = JSON.stringify(data, null, 2);
      element.innerHTML = '';
      element.appendChild(resultDiv);
    }

    async function testHealth() {
      try {
        const response = await fetch(`${BASE_URL}/health`);
        const data = await response.json();
        showResult('health-result', data);
      } catch (error) {
        showResult('health-result', { error: error.message }, true);
      }
    }

    async function testTemplates() {
      try {
        const response = await fetch(`${BASE_URL}/api/agents/templates/list`);
        const data = await response.json();
        showResult('templates-result', data);
      } catch (error) {
        showResult('templates-result', { error: error.message }, true);
      }
    }

    async function testListAgents() {
      try {
        const response = await fetch(`${BASE_URL}/api/agents`);
        const data = await response.json();
        showResult('agents-result', data);
      } catch (error) {
        showResult('agents-result', { error: error.message }, true);
      }
    }

    async function testCreateAgent() {
      try {
        const response = await fetch(`${BASE_URL}/api/agents`, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            name: 'Test Agent from Browser',
            description: 'Created via browser test',
            systemPrompt: {
              type: 'text',
              content: 'You are a test assistant',
            },
            model: 'claude-sonnet-4',
          }),
        });
        const data = await response.json();
        showResult('create-result', data);
      } catch (error) {
        showResult('create-result', { error: error.message }, true);
      }
    }

    async function runAllTests() {
      await testHealth();
      await new Promise(resolve => setTimeout(resolve, 500));
      await testTemplates();
      await new Promise(resolve => setTimeout(resolve, 500));
      await testListAgents();
      await new Promise(resolve => setTimeout(resolve, 500));
      await testCreateAgent();
    }

    // Auto-run health check on load
    window.addEventListener('load', () => {
      testHealth();
    });
  </script>
</body>
</html>

